home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / text / hyper / hsc_source.lha / hsc / source / ugly / args_hlp.c < prev    next >
C/C++ Source or Header  |  1996-09-10  |  4KB  |  189 lines

  1. /*
  2.  * ugly/args_hlp.c
  3.  *
  4.  * sub-module for ugly/args.c
  5.  *
  6.  * ugly argument handling help functions
  7.  *
  8.  * Copyright (C) 1994,95  Thomas Aglassinger
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  *
  24.  * updated: 10-Sep-1996
  25.  * created:  3-Jul-1994
  26.  *
  27.  */
  28.  
  29. /*
  30.  * includes
  31.  */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdarg.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37.  
  38. #include "utypes.h"
  39. #include "umemory.h"
  40. #include "ustring.h"
  41. #include "dllist.h"
  42.  
  43. #define NOEXTERN_UGLY_ARGS_H
  44. #include "uargs.h"
  45.  
  46. /*
  47.  * strcat_flag
  48.  */
  49. static VOID strcat_flag(STRPTR s, struct arginfo *ai, ULONG chk_flag, char ch)
  50. {
  51.     char flag[3] = "/x";
  52.  
  53.     if ((ai->ai_flags) & chk_flag)
  54.     {
  55.         flag[1] = ch;
  56.         strcat(s, flag);
  57.     }
  58. }
  59.  
  60. static STRPTR ai2str(struct arginfo *ai)
  61. {
  62.     static STRARR s[100];
  63.  
  64.     strncpy(s, ai->ai_id, 100 - 20);
  65.  
  66.     switch (ai->ai_type)
  67.     {
  68.     case ARG_SWITCH:
  69.         strcat(s, "/S");
  70.         break;
  71.     case ARG_LONG_RANGE:
  72.     case ARG_LONG:
  73.         strcat(s, "/N");
  74.         break;
  75.     case ARG_TEXT:
  76.     case ARG_ENUM:
  77.     case ARG_HANDLEFUNC:
  78.         break;
  79.     default:
  80.         strcat(s, "/?");
  81.         break;
  82.     }
  83.  
  84.     strcat_flag(s, ai, ARG_KEYWORD, 'K');
  85.     strcat_flag(s, ai, ARG_REQUIRED, 'A');
  86.     strcat_flag(s, ai, ARG_MULTIPLE, 'M');
  87.     strcat_flag(s, ai, ARG_CASESENS, 'C');
  88.  
  89.     return (s);
  90. }
  91.  
  92. /*
  93.  * fprintf_arghelp
  94.  */
  95. int fprintf_arghelp(FILE * stream, struct arglist *al)
  96. {
  97.     int err = 0;
  98.  
  99.     if (al)
  100.     {
  101.         struct dlnode *nd;
  102.         struct arginfo *ai;
  103.         size_t maxidlen = 0;      /* max. length if arg id */
  104.  
  105.         /* compute maximum length */
  106.         nd = al->al_list->first;
  107.         while (nd)
  108.         {
  109.             ai = (struct arginfo *) dln_data(nd);
  110.  
  111.             if (ai)
  112.             {
  113.                 if (ai->ai_help)
  114.                 {
  115.                     STRPTR s = ai2str(ai);
  116.                     if (strlen(s)>maxidlen)
  117.                         maxidlen = strlen(s);
  118.                 }
  119.             }
  120.             nd = dln_next(nd);
  121.         }                       /*while */
  122.         maxidlen += 2;
  123.  
  124.         nd = al->al_list->first;
  125.         while (nd)
  126.         {
  127.             ai = (struct arginfo *) dln_data(nd);
  128.  
  129.             if (ai)
  130.             {
  131.                 if (ai->ai_help)
  132.                 {
  133.                     STRPTR s = ai2str(ai);
  134.  
  135.                     if (ai->ai_help)
  136.                         fprintf(stream, " %-*s  %s",
  137.                                 (int) maxidlen, s, ai->ai_help);
  138.                     else
  139.                         fprintf(stream, "%s", s);
  140.  
  141.                     fprintf(stream, "\n");
  142.                 }
  143.             }
  144.  
  145.             nd = dln_next(nd);
  146.         }                       /*while */
  147.     }
  148.  
  149.     return err;
  150. }
  151.  
  152. /*
  153.  * fprintf_arghelp_short
  154.  *
  155.  * print short template line help
  156.  */
  157. int fprintf_arghelp_short(FILE * stream, struct arglist *al)
  158. {
  159.     int err = 0;
  160.  
  161.     if (al)
  162.     {
  163.         struct dlnode *no;
  164.         struct arginfo *ai;
  165.  
  166.         no = al->al_list->first;
  167.  
  168.         if (no)
  169.             fprintf(stream, "Usage: ");
  170.  
  171.         while (no)
  172.         {
  173.             ai = (struct arginfo *) no->data;
  174.  
  175.             if (ai)
  176.                 fprintf(stream, "%s", ai2str(ai));
  177.  
  178.             no = no->next;
  179.             if (no)
  180.                 fprintf(stream, ",");
  181.         }                       /*while */
  182.  
  183.         fprintf(stream, "\n");
  184.     }
  185.  
  186.     return err;
  187. }
  188.  
  189.